/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.reading.bank.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.entity.Post;
import com.jeesite.modules.sys.service.PostService;
import com.jeesite.modules.zx.reading.bank.entity.TreadingBank;
import com.jeesite.modules.zx.reading.bank.service.TreadingBankService;

/**
 * 阅读题题库Controller
 * @author zpp
 * @version 2020-03-27
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/reading/bank/treadingBank")
public class TreadingBankController extends BaseController {

	@Autowired
	private TreadingBankService treadingBankService;
	@Autowired
	private PostService postService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TreadingBank get(String id, boolean isNewRecord) {
		return treadingBankService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:reading:bank:treadingBank:view")
	@RequestMapping(value = {"list", ""})
	public String list(TreadingBank treadingBank, Model model) {
		model.addAttribute("treadingBank", treadingBank);
		return "modules/zx/reading/bank/treadingBankList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:reading:bank:treadingBank:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TreadingBank> listData(TreadingBank treadingBank, HttpServletRequest request, HttpServletResponse response) {
		treadingBank.setPage(new Page<>(request, response));
		Page<TreadingBank> page = treadingBankService.findPage(treadingBank);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:reading:bank:treadingBank:view")
	@RequestMapping(value = "form")
	public String form(TreadingBank treadingBank, Model model) {
		Post post = new Post();
		model.addAttribute("postList", postService.findList(post));
		model.addAttribute("treadingBank", treadingBank);
		return "modules/zx/reading/bank/treadingBankForm";
	}

	/**
	 * 保存阅读题题库
	 */
	@RequiresPermissions("zx:reading:bank:treadingBank:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TreadingBank treadingBank) {
		treadingBankService.save(treadingBank);
		return renderResult(Global.TRUE, text("保存阅读题题库成功！"));
	}
	
	/**
	 * 停用阅读题题库
	 */
	@RequiresPermissions("zx:reading:bank:treadingBank:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TreadingBank treadingBank) {
		treadingBank.setStatus(TreadingBank.STATUS_DISABLE);
		treadingBankService.updateStatus(treadingBank);
		return renderResult(Global.TRUE, text("停用阅读题题库成功"));
	}
	
	/**
	 * 启用阅读题题库
	 */
	@RequiresPermissions("zx:reading:bank:treadingBank:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TreadingBank treadingBank) {
		treadingBank.setStatus(TreadingBank.STATUS_NORMAL);
		treadingBankService.updateStatus(treadingBank);
		return renderResult(Global.TRUE, text("启用阅读题题库成功"));
	}
	
	/**
	 * 删除阅读题题库
	 */
	@RequiresPermissions("zx:reading:bank:treadingBank:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TreadingBank treadingBank) {
		treadingBankService.delete(treadingBank);
		return renderResult(Global.TRUE, text("删除阅读题题库成功！"));
	}
	
}